﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XLB.ExtensionMethod
{
    public class LogHelper
    {
        /// <summary>
        /// 日志锁
        /// </summary>
        private static object o_Lock = new object();

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="Msg">内容</param>
        public static void WriteLog(string Msg)
        {
            lock (o_Lock)
            {
                try
                {
                    string logFileName = string.Format("{0}.log", "Log\\" + DateTime.Now.ToString("yyyyMM") + "\\" + DateTime.Now.ToString("yyyyMMdd"));
                    FileInfo fi = new FileInfo(logFileName);
                    var di = fi.Directory;
                    if (!di.Exists)
                    {
                        di.Create();
                    }
                    using (StreamWriter sw = new StreamWriter(logFileName, true, Encoding.UTF8))
                    {
                        sw.WriteLine(DateTime.Now.TimeOfDay.ToString() + "  " + Msg);
                    }
                }
                catch
                {

                }
            }
        }
    }
}
